/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package econutri.servicos.recordatorio;

import econutri.util.Utils;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lomtools.persistence.GenericDAO;

/**
 *
 * @author leoomoreira
 */
public class ListarRecordatorioNutrienteXMLServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        response.setContentType("application/xml;charset=ISO-8859-1");
        String pacienteCodigo = request.getParameter("pacienteCodigo");
        String data = request.getParameter("data");
        String xml = "";
        xml += "<nutrientes>";
        try {
            GenericDAO dao = new GenericDAO();
            String sqlNutrientes = "" +
            "select " +
                "pacientecodigo, data, carboidratomin, proteinamin, " +
                "lipidiomin, calciomin, fosforomin, ferromin, sodiomin, " +
                "vitaminaamin, vitaminab1min, vitaminab2min, niacinamin, " +
                "vitaminacmin, fibramin, colesterolmin, carboidratomax, " +
                "proteinamax, lipidiomax, calciomax, fosforomax, " +
                "ferromax, sodiomax, vitaminaamax, vitaminab1max, " +
                "vitaminab2max, niacinamax, vitaminacmax, fibramax, " +
                "colesterolmax " +
            "from recordatorionutriente " +
            "where pacientecodigo = " + pacienteCodigo + " and data = '" + Utils.converterDataSQL(data) + "'" +
                    "";
            ResultSet rsNutrientes = dao.executeQuery(sqlNutrientes);
            dao.close();
            while (rsNutrientes != null && rsNutrientes.next()) {
                xml += "<carboidratomin>" + rsNutrientes.getDouble("carboidratomin") + "</carboidratomin>";
                xml += "<proteinamin>" + rsNutrientes.getDouble("proteinamin") + "</proteinamin>";
                xml += "<lipidiomin>" + rsNutrientes.getDouble("lipidiomin") + "</lipidiomin>";
                xml += "<calciomin>" + rsNutrientes.getDouble("calciomin") + "</calciomin>";
                xml += "<fosforomin>" + rsNutrientes.getDouble("fosforomin") + "</fosforomin>";
                xml += "<ferromin>" + rsNutrientes.getDouble("ferromin") + "</ferromin>";
                xml += "<sodiomin>" + rsNutrientes.getDouble("sodiomin") + "</sodiomin>";
                xml += "<vitaminaamin>" + rsNutrientes.getDouble("vitaminaamin") + "</vitaminaamin>";
                xml += "<vitaminab1min>" + rsNutrientes.getDouble("vitaminab1min") + "</vitaminab1min>";
                xml += "<vitaminab2min>" + rsNutrientes.getDouble("vitaminab2min") + "</vitaminab2min>";
                xml += "<niacinamin>" + rsNutrientes.getDouble("niacinamin") + "</niacinamin>";
                xml += "<vitaminacmin>" + rsNutrientes.getDouble("vitaminacmin") + "</vitaminacmin>";
                xml += "<fibramin>" + rsNutrientes.getDouble("fibramin") + "</fibramin>";
                xml += "<colesterolmin>" + rsNutrientes.getDouble("colesterolmin") + "</colesterolmin>";
                xml += "<carboidratomax>" + rsNutrientes.getDouble("carboidratomax") + "</carboidratomax>";
                xml += "<proteinamax>" + rsNutrientes.getDouble("proteinamax") + "</proteinamax>";
                xml += "<lipidiomax>" + rsNutrientes.getDouble("lipidiomax") + "</lipidiomax>";
                xml += "<calciomax>" + rsNutrientes.getDouble("calciomax") + "</calciomax>";
                xml += "<fosforomax>" + rsNutrientes.getDouble("fosforomax") + "</fosforomax>";
                xml += "<ferromax>" + rsNutrientes.getDouble("ferromax") + "</ferromax>";
                xml += "<sodiomax>" + rsNutrientes.getDouble("sodiomax") + "</sodiomax>";
                xml += "<vitaminaamax>" + rsNutrientes.getDouble("vitaminaamax") + "</vitaminaamax>";
                xml += "<vitaminab1max>" + rsNutrientes.getDouble("vitaminab1max") + "</vitaminab1max>";
                xml += "<vitaminab2max>" + rsNutrientes.getDouble("vitaminab2max") + "</vitaminab2max>";
                xml += "<niacinamax>" + rsNutrientes.getDouble("niacinamax") + "</niacinamax>";
                xml += "<vitaminacmax>" + rsNutrientes.getDouble("vitaminacmax") + "</vitaminacmax>";
                xml += "<fibramax>" + rsNutrientes.getDouble("fibramax") + "</fibramax>";
                xml += "<colesterolmax>" + rsNutrientes.getDouble("colesterolmax") + "</colesterolmax>";
            }

        } catch (SQLException ex) {

        }
        xml += "</nutrientes>";
        PrintWriter out = response.getWriter();
        try {
            out.println(xml);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
